package com.badshun.weapon;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.utils.Array;
import com.badshun.box2d.Box2DWorld;
import com.badshun.player.MainPlayer;
import com.badshun.player.MainPlayer.Direction;
import com.badshun.player.MainPlayer.State;

public class Ball {
	private OrthographicCamera camera;
	private Box2DWorld b2dWorld;
	private MainPlayer player;
	private Wand wand;
	
	public Sprite ballSprite;
	
	public Vector2 position;
	
	private SpriteBatch batch;
	
	public boolean disappear = false;
	
	public Body ballBody;
	
	private long id = System.currentTimeMillis();
	private float speed = 600000000;
	
	private float knobPercentX, knobPercentY;
	
	public Ball(OrthographicCamera camera,Box2DWorld b2dWorld, MainPlayer player, Wand wand, float knobPercentX, float knobPercentY) {
		this.camera = camera;
		this.b2dWorld = b2dWorld;
		this.player = player;
		this.wand = player.wand;
		this.knobPercentX = knobPercentX;
		this.knobPercentY = knobPercentY;
		
		ballSprite = new Sprite(new Texture(Gdx.files.internal("weapon/ball.png")));
		ballSprite.setSize(ballSprite.getWidth() / 4, ballSprite.getHeight() / 4);
		ballSprite.setOrigin(ballSprite.getWidth() / 2, ballSprite.getHeight() / 2);
		
		batch = new SpriteBatch();
		
		position = new Vector2(wand.position.x + ballSprite.getWidth() / 2, wand.position.y + ballSprite.getHeight() / 2);
		ballSprite.setPosition(position.x, position.y);
		
		createBody();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					
					disappear = true;
				} catch (InterruptedException ie) {
					ie.printStackTrace();
				}
			}
		}).start();
	}
	
	private void createBody() {
		BodyDef bDef = new BodyDef();
		bDef.position.set(position.x + ballSprite.getWidth() / 4, position.y + ballSprite.getHeight() / 2);
		bDef.type = BodyDef.BodyType.DynamicBody;
		
		ballBody = b2dWorld.world.createBody(bDef);
		ballBody.setUserData("BALL" + id);
		
		CircleShape shape = new CircleShape();
		shape.setRadius(ballSprite.getWidth() / 2);
		
		FixtureDef fDef = new FixtureDef();
		fDef.shape = shape;
		fDef.filter.categoryBits = 2;
		fDef.filter.maskBits = 4 | 8;
		
		ballBody.createFixture(fDef).setUserData("BALL" + id );
	}
	
	public void move() {
		if(ballBody.getLinearVelocity().x < speed + 1 && ballBody.getLinearVelocity().x > -speed - 1 && ballBody.getLinearVelocity().y < speed + 1 && ballBody.getLinearVelocity().y > -speed - 1) {
			if(knobPercentX == 0 && knobPercentX != 0) {
				ballBody.applyLinearImpulse(new Vector2(-ballBody.getLinearVelocity().x, knobPercentX * speed), ballBody.getWorldCenter(), true);
			} else if(knobPercentX !=0 && knobPercentX == 0) {
				ballBody.applyLinearImpulse(new Vector2(knobPercentX * speed, -ballBody.getLinearVelocity().y), ballBody.getWorldCenter(), true);
			} else if(knobPercentX == 0 && knobPercentX == 0) {
				ballBody.applyLinearImpulse(new Vector2(-ballBody.getLinearVelocity().x, -ballBody.getLinearVelocity().y), ballBody.getWorldCenter(), true);
			} else {
				ballBody.applyLinearImpulse(new Vector2(knobPercentX * speed, knobPercentY * speed), ballBody.getWorldCenter(), true);
			}

		}
	}
	
	public void destroyBody() {
		Array<Body> bodies = new Array<Body>();
		
		b2dWorld.world.getBodies(bodies);
		
		for(int i = 0; i < bodies.size; i++) {
			Array<Fixture> fixtures = bodies.get(i).getFixtureList();
			
			for(int j = 0; j < fixtures.size; j++) {
				if(fixtures.get(j).getUserData() != null) {
					if(fixtures.get(j).getUserData().toString().startsWith("BALL")) {
						String userData = fixtures.get(j).getUserData().toString();
						userData = userData.replace("BALL", "");
						
						if(userData.equals(id + "")) {
							bodies.get(i).destroyFixture(fixtures.get(j));
						}
					}
				}
			}
			
			if(bodies.get(i).getUserData() != null) {
				if(bodies.get(i).getUserData().toString().startsWith("BALL")) {
					String userData = bodies.get(i).getUserData().toString();
					userData = userData.replace("BALL", "");
					
					if(userData.equals(id + "")) {
						b2dWorld.world.destroyBody(bodies.get(i));
					}
				}
			}
		}
	}
	
	public void render() {
		move();
		
		ballSprite.setPosition(ballBody.getPosition().x - ballSprite.getWidth() / 2, ballBody.getPosition().y - ballSprite.getHeight() / 2);
		
		batch.begin();
		batch.setProjectionMatrix(camera.combined);
		ballSprite.draw(batch);
		batch.end();
	}
}
























